package com.shqcjd.siemensglass.http;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by sy on 2018/5/3.
 */

public class ApiManager {
    private  static ApiManager mInstance;
    private  static ApiManager mInstance2;
    private  ApiService mApiService;

    public ApiManager(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HttpUrl.SERVER_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
//                .client(OkHttpUtils.getInstance())
                .client(TokenOkHttpUtils.getInstance())
                .build();
        mApiService = retrofit.create(ApiService.class);

    }

    public ApiManager(Boolean hanToken){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HttpUrl.SERVER_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(TokenOkHttpUtils.getInstance())
                .build();
        mApiService = retrofit.create(ApiService.class);

    }

    public ApiManager(String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(OkHttpUtils.getInstance())
                .build();
        mApiService = retrofit.create(ApiService.class);
    }

    public ApiManager(String url, String name, String pwd){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(AuthOkHttpUtils.getInstance(name,pwd))
                .build();
        mApiService = retrofit.create(ApiService.class);
    }





    public static ApiService getService(){
        if (mInstance == null) {
            mInstance = new ApiManager();
        }
        return mInstance.mApiService;
    }

    public static ApiService getService(String url){
        if (mInstance2 == null) {
            mInstance2 = new ApiManager(url);
        }
        return mInstance2.mApiService;
    }

    public static ApiService getService(String url, String name, String pwd){
        if (mInstance2 == null) {
            mInstance2 = new ApiManager(url,name,pwd);
        }
        return mInstance2.mApiService;
    }



}
